
package com.yuan.unit;

import org.json.JSONArray;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 公共工具类
 * 
 * @author YUAN
 */
public class CommUtils {
    public static boolean checkUserName(String name)
    {
        boolean flag = true;
        return flag;
    }

    public static boolean checkepsd(String name)
    {
        boolean flag = true;
        return flag;
    }

    // 验证是否是正确的json格式
    @SuppressWarnings("unused")
    public static boolean isJson(String str)
    {
        try {
            JSONObject temp = new JSONObject(str);
            JSONArray jsonArray = temp.getJSONArray("STR");
            List<?> list = JsonUtils.getList(str);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 检查离开时间是否正确
     * 
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String checkLeaveTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    @SuppressWarnings("deprecation")
    public static void addViewToWindow(Context context, View view) {
        addViewToWindow(context, view, LayoutParams.FILL_PARENT, Gravity.BOTTOM);
    }

    public static void addViewToWindow(Context context, View view, int height, int gravity) {
        try {
            WindowManager.LayoutParams params = new WindowManager.LayoutParams();
            params.width = LayoutParams.MATCH_PARENT;
            params.height = height;
            params.format = PixelFormat.TRANSPARENT;
            params.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            params.gravity = gravity;
            ((Activity) context).getWindowManager().addView(view, params);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int findStringInlist(List<?> list, String mapItem, String str)
    {
        int pos = -1;
        for (int i = 0; i < list.size(); i++)
        {
            Map<?, ?> map = (Map<?, ?>) list.get(i);
            if (map.get(mapItem).equals(str))
            {
                return i;
            }
        }
        return pos;
    }

    public final static void dumpSystemInfo() {
        StringBuilder buf = new StringBuilder();
        buf.append("====================Dump System Information================================\n");
        buf.append(String.format("DEBUG=%s", SystemInfo.DEBUG));
        buf.append('\n');
        buf.append(String.format("versionCode=%d", SystemInfo.versionCode));
        buf.append('\n');
        buf.append(String.format("versionName=%S", SystemInfo.versionName));
        buf.append('\n');
        buf.append(String.format("deviceId=%S", SystemInfo.deviceId));
        buf.append('\n');
        buf.append(String.format("density=%S", SystemInfo.density));
        buf.append('\n');
        buf.append(String.format("densityDpi=%S", SystemInfo.densityDpi));
        buf.append('\n');
        buf.append(String.format("widthPixels=%S", SystemInfo.widthPixels));
        buf.append('\n');
        buf.append(String.format("heightPixels=%S", SystemInfo.heightPixels));
        buf.append('\n');
        buf.append("====================END Dump System Information============================\n");
        BLog.d("SYSTEMINFO", buf.toString());
    }

}
